package com.agu.result;

import com.agu.utils.PageUtils;
import com.github.pagehelper.Page;

/**
 * 统一返回封装类
 * @author agu
 *
 */
@SuppressWarnings("unchecked")
public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    //成功
    @SuppressWarnings("rawtypes")
	public static Result success() {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }
    @SuppressWarnings("rawtypes")
	public static Result success(String message) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(message);
    }
    //成功
    @SuppressWarnings("rawtypes")
	public static <T> Result<T> successData(T data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }
    //成功 带分页
    @SuppressWarnings("rawtypes")
	public static <T> Result<T> successPage(Page<T> page) {
    	//Page{count=true, pageNum=1, pageSize=10, startRow=0, endRow=10, total=25, pages=3,
    		PageUtils data = new PageUtils(page, (int)page.getTotal(), page.getPageSize(), page.getPageNum());
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }
    
    //登录失败
    @SuppressWarnings("rawtypes")
    public static Result loginError(String message) {
        return new Result()
                .setCode(ResultCodeEnum.LOGIN_ERROR.getCode())
                .setMessage(message);
    }
    //参数失败
    @SuppressWarnings("rawtypes")
    public static Result paramsError(String message) {
        return new Result()
                .setCode(ResultCodeEnum.PARAMS_ERROR.getCode())
                .setMessage(message);
    }
    //失败
    @SuppressWarnings("rawtypes")
    public static Result fail(String message) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL.getCode())
                .setMessage(message);
    }
    //服务器错误
    @SuppressWarnings("rawtypes")
    public static Result serverFail(String message) {
        return new Result()
                .setCode(ResultCodeEnum.SERVER_ERROR.getCode())
                .setMessage(message);
    }
    //未登录
    @SuppressWarnings("rawtypes")
    public static Result unauthorized() {
        return new Result()
                .setCode(ResultCodeEnum.UN_AUTHORIZED.getCode())
                .setMessage("未登录！");
    }
    //没有权限
    @SuppressWarnings("rawtypes")
    public static Result noPermissions() {
        return new Result()
                .setCode(ResultCodeEnum.NO_Permissions.getCode())
                .setMessage("权限不足！");
    }
}
